﻿/*
 * Author: Mattan360
 * Contributors:
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Irrlicht.Net.Core;
using Irrlicht.Net.Video;

namespace Irrlicht.Net.GUI
{
    public sealed class GUIEnvironment : ReferenceCounted
    {
        #region PInvoke
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern bool GUI_DrawAll(IntPtr guienv);

        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr GUI_addStaticText(IntPtr gui,
            [MarshalAs(UnmanagedType.BStr)]
            string text,
            int x, int y, int x2, int y2, bool border, bool wordWrap, int id, bool fillBackground);

        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr GUI_addImage(IntPtr gui, 
            int rectX1, int rectY1, int rectX2, int rectY2, IntPtr parent, int id, 
            [MarshalAs(UnmanagedType.BStr)]
            string text);
 
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr GUI_addImage2(IntPtr gui, 
            IntPtr image, int xPos, int yPos, bool useAlphaChannel, IntPtr parent, int id,
            [MarshalAs(UnmanagedType.BStr)]
            string text);
        #endregion

        internal static GUIEnvironment CreateGUIEnvironment(IntPtr pointer)
        {
            GUIEnvironment environment = new GUIEnvironment();
            environment.pointer = pointer;
            return environment;
        }

        public void DrawAll()
        {
            GUI_DrawAll(pointer);
        }

        public StaticText AddStaticText(string text, Rectangle<int> rect)
        {
            return StaticText.CreateStaticText(GUI_addStaticText(pointer, text, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, false, true, -1, false));
        }
        public StaticText AddStaticText(string text, Rectangle<int> rect, bool border)
        {
            return StaticText.CreateStaticText(GUI_addStaticText(pointer, text, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, border, true, -1, false));
        }

        public Image AddImage(Rectangle<int> rect)
        {
            return new Image(GUI_addImage(pointer, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, (IntPtr) 0, -1, string.Empty));
        }

        public Image AddImage(Rectangle<int> rect, Element parent)
        {
            return new Image(GUI_addImage(pointer, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, parent.pointer, -1, string.Empty));
        }

        public Image AddImage(Rectangle<int> rect, Element parent, int id)
        {
            return new Image(GUI_addImage(pointer, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, parent.pointer, id, string.Empty));
        }

        public Image AddImage(Rectangle<int> rect, Element parent, int id, string text)
        {
            return new Image(GUI_addImage(pointer, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, parent.pointer, id, text));
        }

        public Image AddImage(Texture image, Position2D<int> position)
        {
            return new Image(GUI_addImage2(pointer, image.pointer, position.X, position.Y, true, (IntPtr)0, -1, string.Empty));
        }

        public Image AddImage(Texture image, Position2D<int> position, bool useAlphaChannel)
        {
            return new Image(GUI_addImage2(pointer, image.pointer, position.X, position.Y, useAlphaChannel, (IntPtr) 0, -1, string.Empty));
        }

        public Image AddImage(Texture image, Position2D<int> position, bool useAlphaChannel, Element parent)
        {
            return new Image(GUI_addImage2(pointer, image.pointer, position.X, position.Y, useAlphaChannel, parent.pointer, -1, string.Empty));
        }

        public Image AddImage(Texture image, Position2D<int> position, bool useAlphaChannel, Element parent, int id)
        {
            return new Image(GUI_addImage2(pointer, image.pointer, position.X, position.Y, useAlphaChannel, parent.pointer, id, string.Empty));
        }

        public Image AddImage(Texture image, Position2D<int> position, bool useAlphaChannel, Element parent, int id, string text)
        {
            return new Image(GUI_addImage2(pointer, image.pointer, position.X, position.Y, useAlphaChannel, parent.pointer, id, text));
        }
    }
}
